

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
import email.utils  # 使用官方推荐的工具

qqMail = smtplib.SMTP_SSL("smtp.qq.com", 465)
user = "2184769952@qq.com"  # 确保完全一致
password = "xvxrsuxhsrlgecid"
qqMail.login(user, password)

send = "2184769952@qq.com"  # 必须与user相同
receive = "yequbiancheng@baicizhan.com"

mes = MIMEMultipart()

# === 关键修复：使用email.utils生成RFC兼容格式 ===
mes["From"] = email.utils.formataddr(("小生", send))
mes["To"] = email.utils.formataddr(("夜曲编程", receive))
mes["Subject"] = "测试邮件（带航拍附件）"  # 无需Header类

# 邮件正文
txt = """你好，很高兴见到你，
现在正在进行发送邮件的尝试。
如果收到邮件，请将截图回复给我，谢谢！
附件是我的航拍照片。"""
contentText = MIMEText(txt, "plain", "utf-8")
mes.attach(contentText)

# 附件处理（简化版）
path = r"C:\Users\LAOBIEXL\Desktop\DJI_001\DJI_20250801145319_0080_D.JPG"
with open(path, "rb") as img:
    attach = MIMEImage(img.read())
    
attach.add_header(
    "Content-Disposition",
    "attachment",
    filename="阴山山脉航拍.jpg"  # 英文名更安全
)
mes.attach(attach)

# === 最终发送 ===
try:
    qqMail.sendmail(send, [receive], mes.as_string())
    print("邮件发送成功！ ✅")
except Exception as e:
    print(f"发送失败: {e}")
finally:
    qqMail.quit()  # 确保关闭连接

    import smtplib
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.mime.text import MIMEText
from email.mime.image import MIMEImage

qqMail = smtplib.SMTP_SSL("smtp.qq.com",465)
user = "2184769952@qq.com"
password = "xvxrsuxhsrlgecid"
qqMail.login(user,password)

send = "2184769952@qq.com"
receive = "yequbiancheng@baicizhan.com"

mes = MIMEMultipart()

mes["Subject"] = Header("给的一封邮件")
mes["From"] = Header(f"小生<{send}>","utf-8")
mes["To"] = Header(f"夜q<{receive}>","utf-8")

txt = "你好，很高兴见到你，现在正在进行的是发送邮件的尝试，如果你能收到我的邮件，可以将邮件的截图再发送回来吗，谢谢！另外传输的图片附件就放我的航拍照片吧"
contentText = MIMEText(txt,"plain","utf-8")

path = r"C:\Users\LAOBIEXL\Desktop\DJI_001\DJI_20250801145319_0080_D.JPG"

with open(path,"rb") as img:
    attachment = img.read()

attach = MIMEImage(attachment)
attach.add_header("Content-Disposition", "attachment", filename="阴山山脉航拍图片.jpg")

mes.attach(attach)
mes.attach(contentText)

qqMail.sendmail(send,receive,mes.as_string())
print("发送成功！")